let express = require('express')
const dbConnect = require('./db/mongodbConnect')
//引入UI路由器
const uiRoute = require('./router/uiRoute') 
//引入user业务路由器
const userRoute = require('./router/userRoute')
const petRoute = require('./router/petRoute')
const agencyRoute = require('./router/agencyRoute')
const commentRoute = require('./router/commentRoute')
const typeRoute = require('./router/typeRoute')
const likeRoute = require('./router/likeRoute')
const uploadRoute = require('./router/uploadRoute')
const orderRoute = require('./router/orderRoute')
const bodyParser = require('body-parser');
let app = express()


app.use(bodyParser.urlencoded({
    extended: false
}))
// 设置为true，可以接受任意类型的参数，但body始终为空，可能是与qs有关
app.use(bodyParser.json())

//数据库连接成功后，注册路由
dbConnect.then(() =>{
    //暴露静态资源
    app.use(express.static('dist'))//直接访问login.html,不暴露就直接走路由
    //使用内置中间件用于获取post请求体参数
    app.use(express.urlencoded({extended:true}))//代替bodyparser

    app.use('/upload',express.static('upload')); 
    //使用UI路由器中间件
    app.use('/adopt',uiRoute)
    //使用user路由器中间件
    app.use('/adopt',userRoute)
    app.use('/adopt',petRoute)
    app.use('/adopt',agencyRoute)
    app.use('/adopt',commentRoute)
    app.use('/adopt',typeRoute)
    app.use('/adopt',likeRoute)
    app.use('/adopt',uploadRoute)
    app.use('/adopt',orderRoute)

}).catch((err) =>{
    console.log('数据库连接失败',err)
})


//绑定端口监听
app.listen(3000,(err) =>{
    if(!err) console.log('服务器启动成功！！')
    else console.log(err)
})